package toOffer;

import java.util.ArrayList;
import java.util.Collections;

/**
 * <h3>leetcode</h3>
 *
 * @author BigFaceCat
 * @date 2024/1/31 19:48
 * @description <p>图书整理 I</p>
 */

/**
 * 书店店员有一张链表形式的书单，每个节点代表一本书，节点中的值表示书的编号。
 * 为更方便整理书架，店员需要将书单倒过来排列，就可以从最后一本书开始整理，逐一将书放回到书架上。请倒序返回这个书单链表。
 *
 * 示例 1：
 *
 * 输入：head = [3,6,4,1]
 *
 * 输出：[1,4,6,3]
 */
public class LCR123 {
    public static void main(String[] args) {
        int[] head = {3, 6, 4, 1};
        ListNode node1 = new ListNode(3);
        ListNode node2 = new ListNode(6);
        node1.next = node2;
        ListNode node3 = new ListNode(4);
        node2.next = node3;
        ListNode node4 = new ListNode(1);
        node3.next = node4;

        int[] list = reverseBookList(node1);
        for (int i : list) {
            System.out.print(i + " ");
        }
    }

    /**
     * @param head
     * @return
     */
    public static int[] reverseBookList(ListNode head) {
        ArrayList<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        Collections.reverse(list);
        return list.stream().mapToInt(Integer::intValue).toArray();
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
